<?php
class Tag_Model extends MY_Model {
	var $_table = "tags";
	
	function Tag_Model(){
		parent::Model();
	}
	
	function get($params = array(), $start = -1, $offset = -1){
		$params = $this->_getParams($params);
		$DB = $this->_get($params);
		
		$DB->select ( $this->_table . ".*" );
		
		if ($start != - 1 && $offset != - 1) {
			$DB->limit ( $offset, $start );
		}
		
		$query = $DB->get ();
		
		if((isset($params['id']) && $params['id'] != NULL && $params['id'] !=-1) ||
			(isset($params['url_name']) && $params['url_name'] != NULL)){
				
			if ($query->num_rows() > 0){
				return $query->row();
			}
			
			return FALSE;
				
		}
		
		return $query->result ();
	}
	
	function getCount($params = array()){
		$params = $this->_getParams($params);
		$params["num_nodes"] = FALSE;
		$DB = $this->_get($params);

		return $DB->count_all_results ();
	}
	
	function _getParams($params = array()){
		$defaults = array(
			"id"			=> NULL,
			"url_name"		=> NULL,
			"status" 		=> "visible", //no_visible, all
			"hide_empty"	=> TRUE,
			"num_nodes"		=> FALSE, 

			"order"		=> array(
				"name"	=> "asc"
			)
		);
		
		$params = array_merge($defaults, $params);
		
		return $params;
	}
	
	function _get($params = array()){
		
		$this->db->from ( $this->_table );
		
		if($params['status'] != NULL && $params['status'] != "all" ){
			if($params['status'] == "visible"){
				$this->db->where("$this->_table.visible = 1");
			} else if($params['status'] == "no_visible"){
				$this->db->where("$this->_table.visible = 0");
			}
		}
		
		if($params['id'] != NULL && $params['id'] != -1){
			$this->db->where ( "$this->_table.id", $params['id'] );
		} else if($params['url_name'] != NULL && $params['url_name'] != ""){
			$this->db->where ( "$this->_table.url_name", $params['url_name'] );
		} else{
			
			//@TODO resto de filtros
			
			if($params['order'] != NULL){
				foreach($params['order'] as $field => $direction){
					$this->db->order_by ( "$field $direction" );
				}
			}
			
			if($params['num_nodes'] != NULL || $params['hide_empty']){
				if($params['hide_empty']){
					$this->db->join("nodes_tags", "nodes_tags.tag_id=tags.id");
				} else{
					$this->db->join("nodes_tags", "nodes_tags.tag_id=tags.id", "left");
				}
				$this->db->group_by("tags.id");
			
				$this->db->select("COUNT(nodes_tags.node_id) AS num_nodes");
				
			}
		}
		
		return $this->db;
	}
	
	function create($fields = array()){
		//TODO tratar el titulo de las categorias multiidioma (PROPIEDAD TITLE con multitiidoma?)
		$fields['url_name'] = $this->getValidUrlName($fields['url_name']);
		
		$data = array (
			'name' 		=> $fields['name'],
			'url_name' 	=> $fields['url_name'],
			'visible'	=> $fields['visible']
		);

		$this->db->insert($this->_table, $data);
		
		$id = $this->db->insert_id();
		
		return $id;
	}


	function update($fields = array()){

		$tag = $this->get(array("id"=>$fields['id']));
		
		if($tag != NULL){
			// @TODO revisar
			if($fields['url_name'] != $tag->url_name){
				$fields['url_name'] = $this->getValidUrlName($fields['url_name']);
			}
			
			$data = array (
				'name' 		=> $fields['name'],
				'url_name' 	=> $fields['url_name'],
				'visible'	=> $fields['visible']
			);
	
			$this->db->where('id', $fields['id']);
			$this->db->update($this->_table, $data);
			
			return $fields['id'];
		}
		
		return FALSE;
	}
	
	function delete($id){		
		$this->db->where('id', $id);			
		$this->db->delete($this->_table);
		
		$this->db->where("tag_id", $id);
		$this->db->delete("nodes_tags");
	}
}
?>
